#include <fhe/core/NodeInterface.h>
#include <fhe/core/Node.h>

namespace fhe
{
  namespace core
  {
    NodeInterface::NodeInterface( const std::string& module, const std::string& name )
      : module_( module )
      , name_( name )
    {
    }
    
    std::string NodeInterface::module() const
    {
      return module_;
    }
    
    std::string NodeInterface::name() const
    {
      return name_;
    }
    
    void NodeInterface::addFunc( const IFuncDesc::Ptr& func )
    {
      FHE_ASSERT_MSG( funcs_.find( func->name() ) == funcs_.end(), "duplicate func %s", func->name().c_str() );
      funcs_[ func->name() ] = func;
    }
    
    void NodeInterface::addVar( const IVarDesc::Ptr& var )
    {
      FHE_ASSERT_MSG( vars_.find( var->name() ) == vars_.end(), "duplicate var %s", var->name().c_str() );
      vars_[var->name()] = var;
    }
    
    void NodeInterface::build( Node* node )
    {
      node->addSuper( name() );
      for ( std::map< std::string, IFuncDesc::Ptr >::const_iterator func = funcs_.begin(); func != funcs_.end(); ++func )
      {
        node->addFunc( IFunc::Ptr( func->second->build( node ) ) );
      }
      for ( std::map< std::string, IVarDesc::Ptr >::const_iterator var = vars_.begin(); var != vars_.end(); ++var )
      {
        node->addVar( IVar::Ptr( var->second->build( node ) ) );
      }
    }
  }
}
